package com.qingke.stockdata.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qingke.stockdata.model.User;
import com.qingke.stockdata.service.UserService;

@Controller
public class RegisterController {

	@Autowired
	UserService userService;
	
	@RequestMapping(value = "/register")
	public String register() {
		
		return "register";
	}
	
	@RequestMapping(value = "/saveRegister", method=RequestMethod.POST)
	public String saveRegister(String username,
			String password,
			String aginpassword, 
			String phone,
			String email,
			HttpServletRequest request,
			HttpServletResponse response) {
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setPhone(phone);
		user.setEmail(email);
		
		System.out.println(username+password);
		System.out.println(phone);
		System.out.println(email);
		if(password.equals(aginpassword)){
			
			String registermation =  userService.register(user);
			System.out.println(registermation);
			
			if("yes".equals(registermation)){
				
				return "login";
			}else if("errName".equals(registermation)){
				
				request.setAttribute("errName", "此用户名已被注册，请换个名称重新注册！");
				return "register";
			}else if("errPhone".equals(registermation)){
				
				request.setAttribute("errPhone", "此电话号码已被注册，请换个号码注册！");
				return "register";
			}else if("errEmail".equals(registermation)){
				
				request.setAttribute("errEmail", "此邮箱已被注册，请换个邮箱注册！");
				return "register";
			}
			
		}
		
		return "register";
	}
	
}
